package com.second.controller.loginandregister;

import com.second.model.dto.LAndR.LAndRResult;
import com.second.model.pojo.FUser;
import com.second.service.IFUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("index")
public class LoginController {
    @Autowired
    private IFUserService userService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("login")
    @ResponseBody
    public LAndRResult login(@RequestBody FUser fUser){
        String telNumber = fUser.getTelNumber();
        String password = fUser.getPassword();
        FUser user = userService.login(telNumber);
        if (null == user){
            return new LAndRResult("404","号码不存在");
        }else if (null!=user && !password.equals(user.getPassword())){
            return new LAndRResult("400","密码错误");
        }else if (null!=password && password.equals(user.getPassword())){
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user",user);
            return new LAndRResult("200","登录成功",user);
        }else {
            return null;
        }
    }

    @GetMapping("exit")
    public void exit(){
        httpServletRequest.getSession().invalidate();
        System.out.println("退出登录");
    }

}
